package com.jasiri.project.utils.dialog

import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.jasiri.project.R
import com.jasiri.project.app.App
import com.jasiri.project.databinding.DialogBindCardSubmitBinding
import com.jasiri.project.pro.ProDialog
import com.jasiri.project.utils.event.EventType
import com.jasiri.project.utils.gilde.ImageUtil
import com.jasiri.project.utils.http.HttpHelper
import com.jasiri.lib.baseadapter.JasiriOnItemChildClickListener
import com.jasiri.lib.baseadapter.JasiriRecyclerViewAdapter
import com.jasiri.lib.baseadapter.JasiriViewHolderHelper
import org.json.JSONObject

/**
 * Author : wwdeng
 * Date : 2023/8/1 15:44
 * <p>
 * 每天进步一点点
 * <p>
 * Desc : 绑卡确认dialog
 */
class BindCardSubmitDialog: ProDialog {
    private lateinit var binding: DialogBindCardSubmitBinding
    private var mCallback: IDialogCallback? = null

    var itemAdapter: ItemAdapter? = null
    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun initWindow() {
        val params = window?.attributes
        params?.width = App.getPhoneWidth()
        params?.height = WindowManager.LayoutParams.WRAP_CONTENT
        window?.attributes = params
        window?.setWindowAnimations(R.style.dialog_slide_anim) //修改动画样式
        window?.setGravity(Gravity.BOTTOM)
    }

    override fun getRootView(): View {
        binding = DialogBindCardSubmitBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initView() {
        initRecyclerView()
    }

    override fun initListener() {
        setOnClickListener(R.id.btnConfirm)
    }

    override fun initData() {
    }

    override fun onViewClick(v: View?) {
        dismiss()
        if(mCallback != null){
            mCallback?.onClick(v)
        }
    }

    /**
     * 初始化账户列表
     */
    private fun initRecyclerView() {
        val topDecoration = com.jasiri.project.utils.DensityUtils.dp2px(12f)
        binding.rvAccount.addItemDecoration(
            com.jasiri.project.utils.rv.LinearDecoration(
                0,
                0,
                topDecoration,
                topDecoration
            )
        )
        binding.rvAccount.layoutManager = LinearLayoutManager(context)
        itemAdapter = ItemAdapter(binding.rvAccount)
        binding.rvAccount.adapter = itemAdapter
        binding.rvAccount.itemAnimator = DefaultItemAnimator()
        itemAdapter?.setOnItemChildClickListener(JasiriOnItemChildClickListener { _, childView, position ->
        })
    }

    /**
     * 设置账户列表
     */
    fun setBindAccountList(accountList: MutableList<JSONObject>){
        itemAdapter?.data = accountList
    }

    interface IDialogCallback {
        //按钮点击
        fun onClick(view: View?)
    }

    class ItemAdapter(recyclerView: RecyclerView?) :
        JasiriRecyclerViewAdapter<JSONObject>(recyclerView, R.layout.item_account_delete) {
        override fun setItemChildListener(helper: JasiriViewHolderHelper, viewType: Int) {
            helper.setItemChildClickListener(R.id.cardLayout) //点击Item
        }
        override fun fillData(helper: JasiriViewHolderHelper, position: Int, model: JSONObject) {
            helper.setText(R.id.textName,model.optString("walletDesc"))
            helper.setText(R.id.textNumber,model.optString("walletMobile"))
            val imgLogo = helper.getImageView(R.id.imgLogo)
            ImageUtil.Companion.instance?.loadImageUrl(mContext,imgLogo,model.optString("walletLogo"),R.mipmap.img_holder)
        }
    }

    override fun onStart() {
        super.onStart()
        HttpHelper.addEvent(EventType.DIALOG.DIALOG_USER_ACCOUNT_SUBMIT_CONFIRM_TIPS_SHOW,"")
    }

    override fun dismiss() {
        super.dismiss()
        HttpHelper.addEvent(EventType.DIALOG.DIALOG_USER_ACCOUNT_SUBMIT_CONFIRM_TIPS_DISMISS,"")
    }
}